import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import("@/views/Login.vue"),
    name: "login"
  },
  {
    path: "/home",
    component: () => import("@/views/Home.vue"),
    name: "home"
  },
  {
    path: "/",
    redirect: "/login"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path=="/login"){
   next()
  }else{
    if(localStorage.getItem("token")){
      next()
    }else{
      next("/login")
    }
  }
})
export default router
